﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Doo.Controls;
using System.ComponentModel.Design.Serialization;
using System.Web.UI;
using System.Collections;
using Doo.Scripts;

namespace Doo.Template
{
    public class TemplateEvents : List<Event>, IUseScriptObject
    {
        public string ScriptObjectID { get; set; }
        private Controls.ScriptObject _sobject;
        public Controls.ScriptObject ScriptObject
        {
            get { return _sobject ?? (string.IsNullOrEmpty(ScriptObjectID) ? null : Container.FindControl(ScriptObjectID) as Controls.ScriptObject); }
            set { _sobject = value; }
        }
        /// <summary>
        /// 所属页面
        /// </summary>
        protected Page Page { get; set; }

        /// <summary>
        /// Container
        /// Common Control.Parent
        /// </summary>
        protected Control Container { get; set; }

        public void Init(Page page, Control container)
        {
            this.Page = page;
            this.Container = container;

            page.InitComplete += new EventHandler(page_InitComplete);
        }

        void page_InitComplete(object sender, EventArgs e)
        {
            ScriptObject sobj = ScriptObject;
            
            //OnInitComplete,ResloveTarget
            foreach (var ce in this)
            {
                ce.Target.OnInitComplete(this.Container, sobj);
            }
        }

        public virtual Hashtable ToHashtable()
        {
            Hashtable ht = new Hashtable(this.Count);
            foreach (var item in this)
            {
                ht.Add(item.Name, item.Target.GetClientReference());
            }
            return ht;
        }
    }
}
